#region License
/*
 * Copyright (c) 2007, Matthew Holmes
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * * Neither the name of The Mango Project nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

using Mango.Dockers;

using Mango.Framework.Common;
using Mango.Framework.Controls;

using WeifenLuo.WinFormsUI.Docking;

namespace Mango {
    public partial class MainForm : Form {
        //private delegate void DocumentInvoker (IDocument doc);
        private Project m_Project = null;
        private ProjectExplorer m_ProjectExplorer = null;
        private PropertyView m_PropertyView = null;
        private DocumentProgress m_LoadingForm = null;

        public MainForm () {
            InitializeComponent ();
        }

        private Project Project {
            get {
                return m_Project;
            }
            set {
                m_Project = value;
                SetupProject ();
            }
        }

        protected override void OnLoad (EventArgs e) {
            base.OnLoad (e);

            ImageManager im = new ImageManager ();

            MainDockPanel.SuspendLayout (true);

            m_PropertyView = new PropertyView ();
            m_ProjectExplorer = new ProjectExplorer (m_PropertyView);
            
            m_ProjectExplorer.Show (MainDockPanel, DockState.DockLeft);
            m_PropertyView.Show (m_ProjectExplorer.Pane, DockAlignment.Bottom, 0.45);

            MainDockPanel.ResumeLayout (true, true);

            //Singleton.Ref<DocumentManager> ().DocumentLoading += new EventHandler<DocumentEventArgs> (MainForm_DocumentLoading);
            //Singleton.Ref<DocumentManager> ().DocumentLoaded += new EventHandler<DocumentEventArgs> (MainForm_DocumentLoaded);
        }

        /*void MainForm_DocumentLoading (object sender, DocumentEventArgs e) {
        }*/

        /*void MainForm_DocumentLoaded (object sender, DocumentEventArgs e) {
            this.Invoke (new MethodInvoker (delegate {
                if (m_LoadingForm != null) {
                    m_LoadingForm.Close ();
                }
            }
            ));
        }*/

        private void MainForm_FormClosing (object sender, FormClosingEventArgs e) {
            Singleton.Ref<OgreManager> ().Dispose ();
        }

        private void m_GuiFileExit_Click (object sender, EventArgs e) {
            this.Close ();
        }

        private void LoadCallback (IAsyncResult ar) {
            Project.EndLoad (ar);
            this.Invoke (new MethodInvoker( delegate {
                if(m_LoadingForm != null) {
                    m_LoadingForm.Close ();
                }

                m_ProjectExplorer.Project = m_Project;

                PreviewDocument pw = new PreviewDocument ();
                pw.Text = String.Format ("{0} - Preview", Path.GetFileName (m_Project.Path));
                pw.Show (MainDockPanel);
                pw.InitializePreview ();
            }
            ));

            /*IDocument doc = Singleton.Ref<DocumentManager> ().EndLoadDocument (ar);
            this.Invoke (new DocumentInvoker (delegate (IDocument newDoc) {
                DocumentContainer dc = Singleton.Ref<DocumentManager> ().CreateDocumentPanel (newDoc);
                if (dc == null) {
                    return;
                }

                dc.Document = newDoc;
                PanelContainer panel = new PanelContainer ();
                panel.Text = dc.Title;
                panel.SetPanel (dc);
                panel.Show (MainDockPanel);
            }
            ), doc);*/
        }

        private void SetupProject() {
            this.Text = String.Format ("{0} - Mango", m_Project.Name);
        }

        private void m_GuiFileOpenFile_Click (object sender, EventArgs e) {
            /*m_GuiOpenFile.Filter = "Material Scripts (*.material)|*.material";
            DialogResult dr = m_GuiOpenFile.ShowDialog (this);
            if (dr != DialogResult.OK) {
                return;
            }

            m_LoadingForm = new DocumentProgress ();
            //Singleton.Ref<DocumentManager> ().BeginLoadDocument (m_GuiOpenFile.FileName, new AsyncCallback(LoadCallback), null);
            Workspace.BeginLoad (m_GuiOpenFile.FileName, new AsyncCallback (LoadCallback), null);
            m_LoadingForm.ShowDialog (this);*/
        }

        private void m_GuiFileNewProject_Click (object sender, EventArgs e) {
            Project = new Project ();
            m_ProjectExplorer.Project = m_Project;
        }

        private void m_GuiProjectProperties_Click (object sender, EventArgs e) {
            ProjectProperties props = new ProjectProperties (m_Project);
            props.ShowDialog (this);
        }
    }
}
